package ru.ifmo.sound.jmlbased;

import ru.ifmo.sound.WaveFile;

import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author avhaliullin
 */
public class SamplesLibrary {
    private List<String> knownClasses = new ArrayList<String>();
    private Map<String, List<WaveFile>> files = new HashMap<String, List<WaveFile>>();


    public List<String> listKnownClasses() {
        return new ArrayList<String>(knownClasses);
    }

    public List<WaveFile> getSamples(String className) {
        return new ArrayList<WaveFile>(files.get(className));
    }

    public SamplesLibrary(File workingDir) throws UnsupportedAudioFileException, IOException {
        File[] dirs = workingDir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.isDirectory();
            }
        });

        for (File classDir : dirs) {
            String name = classDir.getName();

            knownClasses.add(name);

            File[] filesInDir = classDir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".wav");
                }
            });
            List<WaveFile> waveFiles = new ArrayList<WaveFile>();
            for (File file : filesInDir) {
                waveFiles.add(new WaveFile(file));
            }
            files.put(name, waveFiles);
        }
    }
}
